package com.yf.aclient.adapter;

import java.util.Date;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.yf.aclient.R;
import com.yf.aclient.threads.DownldThread;
import com.yf.aclient.util.DateUtil;

public class PicViewAdapter extends BaseAdapter {
	private LayoutInflater mInflater;
	private List<JSONObject> mData;
	//private SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	public List<JSONObject> getmData() {
		return mData;
	}

	public PicViewAdapter(Context context, List<JSONObject> myData) {
		mInflater = LayoutInflater.from(context);
		this.mData = myData;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mData.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHandler holder = null;
		if (convertView == null) {
			holder = new ViewHandler();
			convertView = mInflater.inflate(R.layout.picturesitem, null);
			holder.resourceName = (TextView) convertView
					.findViewById(R.id.rsourceName);
			holder.userNick = (TextView) convertView
					.findViewById(R.id.uploadMan);
			holder.uploadTime = (TextView) convertView
					.findViewById(R.id.uploadTime);
			holder.memo = (TextView) convertView.findViewById(R.id.memo);
			holder.downloadTimes = (TextView) convertView
					.findViewById(R.id.downloadTimes);
			holder.download = (ImageButton) convertView.findViewById(R.id.download);
			holder.shareWibo = (ImageButton) convertView
					.findViewById(R.id.shareWibo);
			holder.progress = (TextView) convertView
					.findViewById(R.id.progress);
			convertView.setTag(holder);
		} else {
			holder = (ViewHandler) convertView.getTag();
		}
		try {
			if (mData.get(position).getString("id").equals("#")) {
				holder.memo.setText("没有数据！");
				holder.download.setVisibility(View.GONE);
				holder.shareWibo.setVisibility(View.GONE);
			} else {
				holder.resourceName.setText(mData.get(position).getString(
						"resourceName"));
				holder.userNick
						.setText(mData.get(position).getString("userQq"));
				Long time = mData.get(position).getLong("uploadTime");
				holder.uploadTime.setText(DateUtil.convertDateToString(new Date(time),"yyyy-MM-dd HH:mm:ss"));
				holder.memo.setText(mData.get(position).getString("memo"));
				holder.downloadTimes.setText(mData.get(position).getString(
						"sharedTimes"));
				holder.id = mData.get(position).getLong("id");
				holder.fileUrl = mData.get(position).getString("fileUrl");
				final TextView progress = holder.progress;
				final TextView downloadTimes = holder.downloadTimes;
				final String fileName = holder.resourceName.getText().toString();
				final ViewGroup parentfl=parent;
				final Long  id=holder.id;
				holder.download.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View view) {
						// TODO Auto-generated method stub
						Handler handler = new Handler() {
							public void handleMessage(Message msg) {
										switch (msg.what) {
										case 0:
											String info = (String) msg.obj;
											progress.setText(info);
											break;
										case 1:
											Integer times = Integer
													.valueOf(downloadTimes
															.getText()
															.toString());
											times++;
											String info1 = (String) msg.obj;
											try {
												JSONObject jso=new JSONObject(info1);
												downloadTimes.setText(times
														.toString());
												progress.setText(jso.getString("info"));
												Toast.makeText(parentfl.getContext(), "文件"+jso.getString("fileName")+"已下载到目录"+Environment.getExternalStorageDirectory()+"/Download", Toast.LENGTH_LONG).show();
											} catch (JSONException e) {
												// TODO Auto-generated catch block
												e.printStackTrace();
											}
											break;
										default:
											break;
										}
							}
						};
//						DownloadThread downLoadThread = new DownloadThread(
//								fileUrl, handler);
//						downLoadThread.start();
						String  serverUrl=view.getResources().getString(R.string.serverUrl);
						DownldThread downldThread=new DownldThread(serverUrl,id,fileName, handler);
						downldThread.start();
					}
				});

				holder.shareWibo.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View view) {
						LayoutInflater factory = LayoutInflater.from(view.getContext());
						final View textEntryView = factory.inflate(R.layout.wibomemo,
								null);
						new AlertDialog.Builder(view.getContext())
						.setIcon(R.drawable.alert_dialog_icon)
						.setTitle(R.string.wibo_memo)
						.setView(textEntryView)
						.setPositiveButton(R.string.publish,
								new DialogInterface.OnClickListener() {
									public void onClick(DialogInterface dialog,
											int whichButton) {
										EditText wiboContent=(EditText)textEntryView.findViewById(R.id.wiboContent);

										
										
										
										
									}
								})
						.setNegativeButton(R.string.cancel,
								new DialogInterface.OnClickListener() {
									public void onClick(DialogInterface dialog,
											int whichButton) {
										/* User clicked No so do some stuff */
									}
								}).show();
					}
					
				});
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return convertView;
	}

	public final class ViewHandler {
		public Long id;
		public TextView resourceName;
		public TextView userNick;
		public TextView uploadTime;
		public TextView memo;
		public TextView downloadTimes;
		public ImageButton download;
		public ImageButton shareWibo;
		public TextView progress;
		public String fileUrl;
	}
}
